﻿// ready function is used to make a function available after the document is loaded
$(document).ready(function () {

    //this event is fired when new button is clicked
    $("#gd-new-id").live("click", function () {

        // first of all we will check new button is enabled or not
        if (!$(this).hasClass("gd-disabled-cls")) {

            // store the event for call back
            //this event is stored for later in case of 
            // call back from InTheMiddle Dialog.
            strEventForCallBack = '$("#gd-new-id").click()';

            // In this event we will call the empty detail page by 
            // passing Id equal to zero(0).
            // and we will also remove the softlock by current user if any .
            // call function GdNewClick
            // Parameter Module Name.
            GdNewClick(strModuleName);

        }

    });

    //this event is fired when new button is clicked
    $("#gd-Search-btn-id").live("click", function () {

        // first of all we will check new button is enabled or not
        if (!$(this).hasClass("gd-disabled-cls")) {

            strEventForCallBack = '$("#gd-Search-btn-id").click()';

            GdRefresh(strModuleName, nGbCurrentlySelectedRecord, nGBTabIndex, strGbSortOn, strGbSortType, nGbCurrentPage, nGBPageSize, nGbOrgId, false);

        }

    });

    //this event is fired when cancel button is clicked
    $('#btn-cancel-id').live("click", function () {
        var id = $(this).parent().parent().parent().parent().parent().addClass("gb-hidden-cls");
    });

    $('#btn-Save-id').live("click", function () {

        strEventForCallBack = '$("#btn-Save-id").click()';
        if ($("#frm").valid()) {
            //alert("valid");

            if (!$(this).hasClass("gd-disabled-cls")) {
                var str = $("#frm").serializeArray();//.replace(strModuleName + ".", '"').replace("=", '":"').replace("&", '",');
                var o = {};
                var a = str;
                var arr;
                $.each(a, function (index, element) {
                    var val = element.value;
                    //if (val == "on")
                    //{
                    //    val = true
                    //}
                    if ($('[name="' + element.name + '"]').is(":checkbox")) {
                        if (val == 'on')
                            val = true;
                    }

                    arr = strModuleName.split('/');
                    o[element.name.replace(arr[arr.length - 1] + ".", "")] = val;
                });


                //alert(JSON.stringify(o));


                GdSave(arr, o);

            }
        }
        else {
            //alert("not valid");
            return false;
        }
    });


    //this event is fired when delete button is clicked
    $("#gd-delete-id").live("click", function () {
        //alert("delete");
        // first of all we will check delete button is enabled or not
        if (!$(this).hasClass("gd-disabled-cls")) {
            //alert("delete in");
            //strIds contains all those ids,which are to be deleted in a comma seperated manner
            var strIds = $("#tbl-gd-main").GetSelectedIds();

            // open the delete confirmation dialog and 

            $("#dialog-delete-confirm").data('Ids', strIds).dialog("open");


        }
    });

    //this event is fired when copy button is clicked
    $("#gd-copy-id").live("click", function () {

        // first of all we will check copy button is enabled or not
        if (!$(this).hasClass("gd-disabled-cls")) {
            // store the event for call back
            //this event is stored for later in case of 
            // call back from InTheMiddle Dialog.
            strEventForCallBack = '$("#gd-copy-id").click()';
            if (GdDestroy(true)) {


                //strIds contains all those ids,which are to be deleted in a comma seperated manner
                var strIds = $("#tbl-gd-main").GetSelectedIds();

                // open the copy confirmation dialog and 
                // send the comma seperated ids to dialog as data parameter.
                $("#dialog-copy-confirm").data('Ids', strIds).dialog("open");
            }
        }

    });

    //this event is fired when paste button is clicked
    $("#gd-paste-id").live("click", function () {

        // first of all we will check paste button is enabled or not
        if (!$(this).hasClass("gd-disabled-cls")) {

        }
    });

    //this event is fired when first button is clicked
    $("#ahr-gd-first-id").live("click", function () {

        // first of all we will check first button is enabled or not
        if ($(this).hasClass("gd-disabled-cls") == false) {

            // store the event for call back
            //this event is stored for later in case of 
            // call back from InTheMiddle Dialog.
            strEventForCallBack = '$("#ahr-gd-first-id").click()';

            if (GdDestroy(true)) {
                nGbCurrentlySelectedRecord = -1;
                nGbCurrentPage = 1;

                // Call GdRefresh to rebuild the Grid.
                GdRefresh(strModuleName, nGbCurrentlySelectedRecord, nGBTabIndex, strGbSortOn, strGbSortType, nGbCurrentPage, nGBPageSize, nGbOrgId, false);


            }

        }

    });

    //this event is fired when last button is clicked
    $("#ahr-gd-last-id").live("click", function () {

        // first of all we will check last button is enabled or not
        if ($(this).hasClass("gd-disabled-cls") == false) {

            // store the event for call back
            //this event is stored for later in case of 
            // call back from InTheMiddle Dialog.
            strEventForCallBack = '$("#ahr-gd-last-id").click()';

            if (GdDestroy(true)) {

                nGbCurrentlySelectedRecord = -1;
                nGbCurrentPage = nGbTotalPages;

                // Call GdRefresh to rebuild the Grid.
                GdRefresh(strModuleName, nGbCurrentlySelectedRecord, nGBTabIndex, strGbSortOn, strGbSortType, nGbCurrentPage, nGBPageSize, nGbOrgId, false);


            }

        }

    });

    //this event is fired when prev button is clicked
    $("#ahr-gd-prev-id").live("click", function () {

        // first of all we will check prev button is enabled or not
        if ($(this).hasClass("gd-disabled-cls") == false) {

            // store the event for call back
            //this event is stored for later in case of 
            // call back from InTheMiddle Dialog.
            strEventForCallBack = '$("#ahr-gd-prev-id").click()';
            if (GdDestroy(true)) {

                nGbCurrentlySelectedRecord = -1;
                nGbCurrentPage = parseInt(nGbCurrentPage) - 1;

                // Call GdRefresh to rebuild the Grid.
                GdRefresh(strModuleName, nGbCurrentlySelectedRecord, nGBTabIndex, strGbSortOn, strGbSortType, nGbCurrentPage, nGBPageSize, nGbOrgId, false);


            }

        }

    });

    //this event is fired when next button is clicked
    $("#ahr-gd-next-id").live("click", function () {

        // first of all we will check next button is enabled or not
        if ($(this).hasClass("gd-disabled-cls") == false) {

            // store the event for call back
            //this event is stored for later in case of 
            // call back from InTheMiddle Dialog.
            strEventForCallBack = '$("#ahr-gd-next-id").click()';
            if (GdDestroy(true)) {

                nGbCurrentlySelectedRecord = -1;

                nGbCurrentPage = parseInt(nGbCurrentPage) + 1;

                // Call GdRefresh to rebuild the Grid.
                GdRefresh(strModuleName, nGbCurrentlySelectedRecord, nGBTabIndex, strGbSortOn, strGbSortType, nGbCurrentPage, nGBPageSize, nGbOrgId, false);
            }



        }
    });

    //this event is fired each and everytime when any grid  row is clicked
    $(".tr-gd-row-cls").live("click", function (e) {

        // stop event propagation
        // so that this not propagated through its parent
        e.stopPropagation();

        // first of all we will check if row is already
        // selected or not.
        // if not selected then Refresh the grid.



        if (!$(this).hasClass("selected-row-color-cls") || $("#td-row-detail-" + $(this).attr("id").replace("tr-row-", "")).html() == "") {


            // store the event for call back
            //this event is stored for later in case of 
            // call back from InTheMiddle Dialog.
            strEventForCallBack = '$("#' + $(this).attr("id") + '").click()';

            if (nGbCurrentlySelectedRecord != 0) {
                var isDestroy = GdDestroy(true);
            }
            else {
                isDestroy = true;
            }



            if (isDestroy) {
                nGbCurrentlySelectedRecord = parseInt($(this).attr("id").replace("tr-row-", ""));

                // Call GdRefresh to rebuild the Grid.
                GdRefresh(strModuleName, nGbCurrentlySelectedRecord, nGBTabIndex, strGbSortOn, strGbSortType, nGbCurrentPage, nGBPageSize, nGbOrgId, true);
            }
        }
        else {
            var id = $(this).attr("id").replace("tr-row-", "");

            if ($("#tr-row-detail-" + id).hasClass("gb-hidden-cls")) {

                $("#tr-row-detail-" + id).removeClass("gb-hidden-cls");
            }
            else {
                $("#tr-row-detail-" + id).addClass("gb-hidden-cls");
            }

        }
    });

    //this event is fired when any check box is clicked
    $(".chk-gd-row-cls").live("click", function (ev) {

        var bAllChecked = true;
        var strRowIds = $("#tbl-gd-main").GetDataIds().split(",");
        var nCount = 0;

        //this loop loops through each and every row
        for (var i = 0; i < strRowIds.length; i++) {

            //this each loop checks whether any row is unchecked or not,if unchecked,the assigns bAllChecked to false
            $("#tr-row-" + strRowIds[i], "#tbl-gd-main").each(function (index, ui) {

                //Check if all the enabled check box is checked or not.
                if (!$(ui).children("td").children(".chk-gd-row-cls").hasClass("gd-disabled-cls")) {
                    if (!$("td:first-child input", $(ui)).is(":checked")) {
                        bAllChecked = false;
                    }
                }
            });
        }

        //if all the checkboxes on grid are checked,the check the header checkbox
        if (bAllChecked) {
            $(".chk-gd-header-cls").prop("checked", true);
        }
            //if all the checkboxes on grid are not checked,then uncheck the header checkbox
        else {
            if ($(".chk-gd-header-cls").prop("checked")) {
                $(".chk-gd-header-cls").removeAttr("checked", "checked");
            }
        }

        //get the count of all checked checkbox except header.
        $(".chk-gd-row-cls").each(function () {
            if ($(this).is(":checked")) {
                nCount = nCount + 1;
            }
        });

        // if selected checkbox count greater than 1,
        // then enable the delete button. else disable the
        // delete button.
        if (nCount > 0) {
            if ($("#gd-delete-id").hasClass("gd-disabled-cls")) {
                $("#gd-delete-id").removeClass("gd-disabled-cls");
            }
        }
        else {
            if (!$("#gd-delete-id").hasClass("gd-disabled-cls")) {
                $("#gd-delete-id").addClass("gd-disabled-cls");
            }
        }

        // stop event propagation
        // so that this not propagated through its parent
        ev.stopPropagation();

    });

    //this event is fired when td is clicked which contains check box,this event is written to avoid event propagation
    $(".td-gd-chk-cls").live("click", function (ev) {

        // stop event propagation
        // so that this not propagated through its parent
        ev.stopPropagation();
    });

    //this event is fired when any check box header is clicked
    $(".chk-gd-header-cls").live("click", function () {


    });

    //this event is fired for reload the grid for latest data
    $(".reload").live("click", function () {
        // Call GdRefresh to rebuild the Grid.
        GdRefresh(strModuleName, nGbCurrentlySelectedRecord, nGBTabIndex, strGbSortOn, strGbSortType, nGbCurrentPage, nGBPageSize, nGbOrgId, false)
    });

    //this event is fired when header is clicked for sorting
    $(".grid-sort-cls").live("click", function () {

        // store the event for call back
        //this event is stored for later in case of 
        // call back from InTheMiddle Dialog.
        strEventForCallBack = '$("#' + $(this).attr("id") + '").click()';
        if (GdDestroy(false)) {
            // set default sorting class and remove ascending and desending sorting classes.
            $(".tr-gd-header-cls").children("th").each(function () {

                if ($(this).hasClass("headerSortUp") || $(this).hasClass("headerSortDown")) {
                    $(this).removeClass("headerSortUp").removeClass("headerSortDown").addClass("grid-sort-cls");
                }
            });
            strGbSortType = "asc";
            strGbSortOn = $(this).attr("id").replace("th-gd-", "");

            // Call GdRefresh to rebuild the Grid.
            GdRefresh(strModuleName, nGbCurrentlySelectedRecord, nGBTabIndex, strGbSortOn, strGbSortType, nGbCurrentPage, nGBPageSize, nGbOrgId, false);

        }
    });

    //this event is fired when header is clicked for asending sorting
    $(".headerSortUp").live("click", function () {

        // store the event for call back
        //this event is stored for later in case of 
        // call back from InTheMiddle Dialog.
        strEventForCallBack = '$("#' + $(this).attr("id") + '").click()';

        if (GdDestroy(false)) {
            strGbSortType = "desc";
            strGbSortOn = $(this).attr("id").replace("th-gd-", "");

            // Call GdRefresh to rebuild the Grid.
            GdRefresh(strModuleName, nGbCurrentlySelectedRecord, nGBTabIndex, strGbSortOn, strGbSortType, nGbCurrentPage, nGBPageSize, nGbOrgId, false);
        }
    });

    //this event is fired when header is clicked for desending sorting
    $(".headerSortDown").live("click", function () {

        // store the event for call back
        //this event is stored for later in case of 
        // call back from InTheMiddle Dialog.
        strEventForCallBack = '$("#' + $(this).attr("id") + '").click()';

        if (GdDestroy(false)) {
            strGbSortType = "asc";
            strGbSortOn = $(this).attr("id").replace("th-gd-", "");

            // Call GdRefresh to rebuild the Grid.
            GdRefresh(strModuleName, nGbCurrentlySelectedRecord, nGBTabIndex, strGbSortOn, strGbSortType, nGbCurrentPage, nGBPageSize, nGbOrgId, false);
        }
    });

    //this code runs everytime whenever header checkbox is clicked
    $(".chk-gd-header-cls").live("click", function () {

        //this check,checks,if header checkbox is unchecked,uncheck all checked checkboxes
        if (!$(".chk-gd-header-cls").prop("checked")) {
            $("tr:not('.first-row-cls') td:first-child input", "#tbl-gd-main").each(function (idx, ui) {
                $(ui).removeAttr("checked", "checked");
            });
        }

        //call CheckAllRows() function
        CheckAllRows();


    });

    //this event is fired when create is clicked
    $("#btn-create-id").live("click", function () {

        //submit the form which contains the tabs data
        $("#form-general-auth-tabs").submit();

    });

    // this event is fire when particular page is selected from pagelist dropdown.
    $("#sl-gd-page-no-id").die("change").live("change", function () {

        // store the event for call back
        //this event is stored for later in case of 
        // call back from InTheMiddle Dialog.
        strEventForCallBack = '$("#sl-gd-page-no-id").change()';
        var nCurrentPage = parseInt($("#sl-gd-page-no-id").val());

        if (GdDestroy(true)) {

            nGbCurrentPage = nCurrentPage;

            nGbCurrentlySelectedRecord = -1;

            // Call GdRefresh to rebuild the Grid.
            GdRefresh(strModuleName, nGbCurrentlySelectedRecord, nGBTabIndex, strGbSortOn, strGbSortType, nGbCurrentPage, nGBPageSize, nGbOrgId, false);
        }

    });


    // this event is fire when particular page is selected from pagelist dropdown.
    $("#sl-gd-page-size-id").die("change").live("change", function () {

        // store the event for call back
        //this event is stored for later in case of 
        // call back from InTheMiddle Dialog.
        strEventForCallBack = '$("#sl-gd-page-size-id").change()';

        if (GdDestroy(false)) {
            // Call GdRefresh to rebuild the Grid.
            GdRefresh(strModuleName, nGbCurrentlySelectedRecord, nGBTabIndex, strGbSortOn, strGbSortType, nGbCurrentPage, nGBPageSize, nGbOrgId, false);
        }

    });

    // this event fire when user click on back to list button
    $("#btn-back-to-list-id").live("click", function () {

        // store the event for call back
        //this event is stored for later in case of 
        // call back from InTheMiddle Dialog.
        strEventForCallBack = '$("#btn-back-to-list-id").click()';

        // Call GdRefresh to rebuild the Grid.
        GdRefresh(strModuleName, nGbCurrentlySelectedRecord, nGBTabIndex, strGbSortOn, strGbSortType, nGbCurrentPage, nGBPageSize, nGbOrgId, false);

    });

    // theis event is fired when the user wants to hard lock the detail
    $("#btn-setHardLock-id").live("click", function () {
        //alert();
        // first of all we will check new button is enabled or not
        if (!$(this).hasClass("gd-disabled-cls")) {
            // store the event for call back
            //this event is stored for later in case of 
            // call back from InTheMiddle Dialog.
            strEventForCallBack = '$("#btn-setHardLock-id").click()';

            var id = $(this).attr("value");
            //alert(id);
            GdSetHardLock(strModuleName, id);

        }
    });


    // theis event is fired when the user wants to remove hard lock
    $("#btn-releaseHardLock-id").live("click", function () {
        //alert();
        // first of all we will check new button is enabled or not
        if (!$(this).hasClass("gd-disabled-cls")) {
            // store the event for call back
            //this event is stored for later in case of 
            // call back from InTheMiddle Dialog.
            strEventForCallBack = '$("#btn-releaseHardLock-id").click()';

            var id = $(this).attr("value");
            //alert(id);
            GdReleaseHardLock(strModuleName, id);
        }
    });

});
